import sys
from PySide6.QtWidgets import QLineEdit,QCompleter,QMessageBox,QApplication,QDialog,QVBoxLayout
from PySide6.QtCore import Qt,QModelIndex,QObject
from PySide6.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
from PySide6 import QtCore
import config_json
class TodoModel(QtCore.QAbstractListModel):
    def __init__(self, *args, todos=None, **kwargs):
        super(TodoModel, self).__init__(*args, **kwargs)
        self.todos = todos or []
    def data(self, index, role):
        if role == Qt.DisplayRole:
            text = self.todos[index.row()]
            return text
    def rowCount(self, index):
        return len(self.todos)
class AutoItem(QObject):
    item_selected = Signal(int, name='item_selected')
    def __init__(self, le,*args, **kwargs):
        super().__init__(*args, **kwargs)
        self.model=config_json.setting["ffbh"]
        self.le=le
        # 增加自动补全
        self.completer1 = QCompleter(self.model)
        # self.completer1.setCompletionColumn(2)
        # 设置匹配模式  有三种： Qt.MatchStartsWith 开头匹配（默认）  Qt.MatchContains 内容匹配  Qt.MatchEndsWith 结尾匹配
        self.completer1.setFilterMode(Qt.MatchFlag.MatchContains)
        self.completer1.setCaseSensitivity(Qt.CaseInsensitive)
        # 设置补全模式  有三种： QCompleter.PopupCompletion（默认）  QCompleter.InlineCompletion   QCompleter.UnfilteredPopupCompletion
        self.completer1.setCompletionMode(QCompleter.CompletionMode.PopupCompletion)
        # self.completer1.setModel(self.model)
        # 给lineedit设置补全器
        self.le.setCompleter(self.completer1)
        self.completer1.activated.connect(self.complete_actived)
    def complete_actived(self,index):
        print(index)#completeModel index
        src_index=self.completer1.completionModel().mapToSource(index)#model index
        row=src_index.row();#row 
        if(row==-1) :
            return
        rec = self.model.record(row)#record
        print(rec.value(0))#id 
        self.item_selected.emit(rec.value(0))
class Dialog1(QDialog):
    def __init__(self,parent):
        super().__init__(parent)
        verticalLayout = QVBoxLayout(self)
        l=QLineEdit()
        a = AutoItem(l)
        verticalLayout.addWidget(l)
        self.resize(600,600)    
# class Dialog2(QDialog):
#     def __init__(self,parent):
#         super().__init__(parent)
#         verticalLayout = QVBoxLayout(self)
#         l=QLineEdit()
#         ls=config_json.setting["ffbh"]
#         comp1= QCompleter(ls,self)
#         l.setCompleter(comp1)
#         verticalLayout.addWidget(l)
#         self.resize(600,600)           
# def test2():
#     ls=config_json.setting["ffbh"]
#     comp1= QCompleter(ls)
#     l=QLineEdit()
#     l.setCompleter(comp1)
#     l.show()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    config_json.read_setting()
    d=Dialog1(None)
    d.show()
    sys.exit(app.exec())
